import React from 'react';
import { message } from 'antd';
import { useRequest, defaultFormatResult } from '@/hooks';
import { getInterviewArrange, acceptInterview } from '../service/interview';
import { pushRoute } from '../utils';

const useInterview = (interviewNoticeId, candidateLinkId) => {
    const { data, loading, run: fetchDetail } = useRequest(() => getInterviewArrange(interviewNoticeId), {
        manual: true,
        initialData: null,
        ...defaultFormatResult
    });

    const { loading: submitLoading, run: handleAccept } = useRequest(() => acceptInterview(interviewNoticeId), {
        manual: true,
        onSuccess: res => {
            const { code, msg } = res;
            if (code === 10000) {
                fetchDetail();
            } else if (msg) {
                message.error(msg);
            }
        }
    });

    const handleCheck = () => {
        console.log('handleCheck', interviewNoticeId, candidateLinkId);
        window.open(`/recruitment/register?id=${candidateLinkId}&nid=${interviewNoticeId}`, '_blank');
        // pushRoute({
        //     pathname: '/recruitment/register',
        //     query: {
        //         id: candidateLinkId,
        //         nid: interviewNoticeId
        //     }
        // });
    };

    const handleRefuse = () => {
        pushRoute({
            pathname: '/recruitment/refuse',
            query: {
                id: candidateLinkId,
                nid: interviewNoticeId
            }
        });
    };

    const handleUpdate = () => {
        pushRoute({
            pathname: '/recruitment/changeInterview',
            query: {
                id: candidateLinkId,
                nid: interviewNoticeId
            }
        });
    };

    React.useEffect(() => {
        if (interviewNoticeId) {
            fetchDetail(interviewNoticeId);
        }
    }, [interviewNoticeId]);

    return {
        status: data?.intervieweeOperateType || 0,
        data,
        loading,
        submitLoading,
        fetchDetail,
        handleAccept,
        handleRefuse,
        handleUpdate,
        handleCheck
    };
};

export default useInterview;
